/**
 * Created by Administrator on 2014/12/24.
 */

package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.ChatActivity;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pojo.getFriendListPojo;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.Message;

import java.util.ArrayList;
import java.util.List;


public class MainMessageAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{

    private Context context;
    private List<Message> msgList;



    public MainMessageAdapter(Context context,List<Message> list){

        this.context = context;
        this.msgList = new ArrayList<Message>();
        this.msgList.addAll(list) ;

//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没，如果没有吃今晚约个时间","12月16号"));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","今晚出去不","12月15号"));

    }



    @Override
    public int getCount() {
        return msgList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        try{
            ViewHolder holder;
            if(view == null){
                //新建一个holder
                view =  LayoutInflater.from(context).inflate(R.layout.page_main_message_adapter_item, viewGroup, false);
                holder = new ViewHolder();

                holder.headImg = (ImageView) view.findViewById(R.id.message_adapter_item_head);
                holder.nickName = (TextView)view.findViewById(R.id.message_adapter_item_nickname);
                holder.say = (TextView)view.findViewById(R.id.message_adapter_item_say);
                holder.time = (TextView)view.findViewById(R.id.message_adapter_item_time);
                view.setTag(holder);

            }else{
                holder = (ViewHolder)view.getTag();
            }

            //初始化所有view
            Message msg = msgList.get(position);

            String nickName = msg.getAccount();
            String tempNickName = msg.getNickName();
            if(tempNickName!=null&&!tempNickName.equals("")){
                nickName = tempNickName;
            }
            holder.nickName.setText(nickName);
            holder.say.setText(msg.getSay());
            holder.time.setText(msg.getTime());


        }catch (Exception e){
            LogToSd.error(e.getMessage());
        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
        Bundle bundle = new Bundle();//该类用作携带数据
        bundle.putInt("flag",0);
        Intent intent = new Intent();
        intent.setClass(context, ChatActivity.class);
        intent.putExtra("data", bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    class ViewHolder{
        ImageView headImg;
        TextView nickName;
        TextView say;
        TextView time;
    }



    public void notifyChange(List<Message> list){
        this.msgList.clear();
        this.msgList.addAll(list);
        this.notifyDataSetChanged();
    }



}
